LootMe = {}

local version = "1.0";

local onLootWindowShown
local isLootMeRunning

function LootMe.Initialize()
  LootMe.Disable()

  onLootWindowShown = EA_Window_Loot.OnShown
  EA_Window_Loot.OnShown = LootMe.OnLootWindowShown

  LibSlash.RegisterSlashCmd("lootme", LootMe.HandleSlashCommand)	
end

function LootMe.OnLootWindowShown(...)
  if not isLootMeRunning then
    return onLootWindowShown(...)
  end

  -- do stuff here
  EA_Window_Loot.OnLootAll()
  return onLootWindowShown(...)
end

function LootMe.parseArguments(delimiter,str)
  if (delimiter=='') then 
    return false 
  end
  
  local pos = 0
  local arr = {}
  local args = string.find(str,delimiter,pos,true)
  for st,sp in args do
    d(st..","..sp);
    table.insert(arr,string.sub(str,pos,st-1)) 
    pos = sp + 1
  end
  table.insert(arr,string.sub(str,pos))
  return arr
end

function LootMe.HandleSlashCommand(input)
  local args = LootMe.parseArguments(" ",input);
  if args[1] == "help" then
    LootMe.displayHelp();
  elseif args[1] == "on" then
    LootMe.Enable()
  elseif args[1] == "off" then
    LootMe.Disable()
  else
    LootMe.DisplayHelp();
  end  
end

function LootMe.DisplayHelp()
  LootMe.WriteToChat("LootMe addon Help");
  LootMe.WriteToChat("Version: "..version);
  LootMe.WriteToChat("Usage: /looter [arguments]");
  LootMe.WriteToChat("/looter help - display this help message");
  LootMe.WriteToChat("/looter [on/off] - turn the LootMe addon on/off");
end

function LootMe.Enable()
  isLootMeRunning = true
  LootMe.WriteToChat("LootMe addon is enabled");
end

function LootMe.Disable()
  isLootMeRunning = false
  LootMe.WriteToChat("LootMe addon is disabled");
end

function LootMe.WriteToChat(text)
  EA_ChatWindow.Print(towstring(text));
end
